package br.com.dallasmotopecas.util.lang;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public final class StringUtil
{
	public static String getStringEspacoExcedente(String stringEntrada)
	{
		StringBuilder sb = null;
		StringTokenizer token = null;
		try
		{
			if (stringEntrada != null)
			{
				sb = new StringBuilder();
				token = new StringTokenizer(stringEntrada);
				
				while (token.hasMoreTokens())
					sb.append(token.nextToken() + " ");
				
				return sb.toString().trim();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (sb != null)
				sb = null;
			
			if (token != null)
				token = null;
		}
		return "";
	}

	public static String getStringformatada(String stringEntrada, String mascara)
	{
		StringBuffer sb = null;
		try
		{
			sb = new StringBuffer();
			int posicaoNumero = 0;
			for (int posicao = 0; posicao < mascara.length(); posicao++)
			{
				if (String.valueOf(mascara.charAt(posicao)).trim().equals("#"))
				{
					sb.append(String.valueOf(stringEntrada.charAt(posicaoNumero)));
					posicaoNumero++;
				}
				else
					sb.append(String.valueOf(mascara.charAt(posicao)));
			}
			return sb.toString();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (sb != null)
				sb = null;
		}
		return "";
	}

	public static String getSomenteNumeros(String stringEntrada)
	{
		StringBuffer sb = null;
		try
		{
			sb = new StringBuffer();
			
			for (int i = 0; i < stringEntrada.length(); i++)
			{
				if (Character.isDigit(stringEntrada.charAt(i)))
					sb.append(String.valueOf(stringEntrada.charAt(i)));
			}
			
			return sb.toString();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (sb != null)
				sb = null;
		}
		return "";
	}
	
	public static String getStringComInicialMaiusculo(String stringEntrada)
	{
		StringBuilder sb = null;
		StringTokenizer token = null;
		List<String> listaString = null;
		String palavra = "";
		String palavraFormatada = "";
		try
		{
			if (stringEntrada != null)
			{
				listaString = new ArrayList<String>();
				listaString.add("de");
				listaString.add("da");
				listaString.add("dos");
				listaString.add("do");
				listaString.add("e");
				sb = new StringBuilder();
				token = new StringTokenizer(stringEntrada);
				
				while (token.hasMoreTokens())
				{
					palavra = token.nextToken();
					if (!listaString.contains(palavra.toLowerCase()))
					{
						palavraFormatada = palavra.substring(0, 1).toUpperCase();
						palavraFormatada += palavra.substring(1, palavra.length()).toLowerCase();
						palavra = palavraFormatada;
					}
					else
					{
						palavraFormatada = palavra.toLowerCase();
						palavra = palavraFormatada;
					}
					
					sb.append(palavra + " ");
				}
				
				return sb.toString().trim();
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (sb != null)
				sb = null;
			
			if (token != null)
				token = null;
		}
		return "";
	}
}
